import request from '../service/api'
import Taro from "@tarojs/taro";

const delay = (time) => new Promise((resolve) => setTimeout(resolve, time || 0))
export default {
  namespace: 'fleet',
  state: {
    current: 0,
    pageNum: 1,
    total: 0,
    fleetAccountId: '',
    currentCar: {},
    carList: [
      // {
      //   bindType: "1",
      //   bindTypeId: 1661,
      //   carNumber: "浙AD33183",
      //   carPhoneNumber: null,
      //   chargeStation: null,
      //   createTime: "2024-12-23T16:06:20",
      //   id: 22,
      //   ruleId: -1,
      //   status: 0,
      //   tenantId: null,
      //   updateTime: "2025-02-27T09:07:13",
      //   vin: "LSVCZ6A40FN033396",
      // },
      // {
      //   bindType: "1",
      //   bindTypeId: 1661,
      //   carNumber: "豫AD33183",
      //   carPhoneNumber: null,
      //   chargeStation: null,
      //   createTime: "2024-12-23T16:06:20",
      //   id: 23,
      //   ruleId: -1,
      //   status: 0,
      //   tenantId: null,
      //   updateTime: "2025-02-27T09:07:13",
      //   vin: "LSVCZ6A40FN033398",
      // }
    ],
    myCar: {
     //  carNum: '豫A38C8T',
     //  brand: '一汽大众',
     //  detailInfo:{
     //    detail: '2020款 一汽大众 探岳GTE 1.4T 双离合变速器 豪华版'
     // },
    }
  },
  effects: {
    *init({payload}, {call, put}){
      const { fleetAccountId } = payload;
      const {data: {code, data: {records = [], total} = {}} = {}} = yield call(() =>
          request.post('/my/fleet/page', {id: fleetAccountId, pageNum: 1, pageSize: 20})); //9999
      if (code && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            carList: records,
            total,
            fleetAccountId
          }
        })
      }
    },
    * loadMore({payload}, {call, put, select}) {
      const {carList, pageNum, fleetAccountId} = yield select(({order}) => order)
      const {data: {code, data: {records = []} = {}} = {}} = yield call(() =>
          request.post('/my/fleet/page', {id: fleetAccountId, pageNum: pageNum + 1, pageSize: 20})); //9999
      if (code && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            carList: carList.concat(records),
            pageNum: pageNum + 1
          }
        })
      }
    },
    *jumpCar(_, {call, put}){
      const {data, statusCode} = yield call(() => request.get('/my/fleet/page'));
      console.log('车辆', data);
      if (statusCode && statusCode === 200){
        yield put({
          type: 'setState',
          payload:{
            myCar: data?.data[0]
          }
        })
        // Taro.navigateTo({
        //   url: '/moduleB/pages/cars/index'
        // })
      } else {
        Taro.showToast({
          title: '车辆信息获取失败,请稍候重试',
          icon: 'none',
          duration: 1500
        })
      }
    },
    *addCar({payload}, {call, select, put}){
      console.log('payload', payload);
      const {fleetAccountId} = yield select(({fleet}) => fleet)
      const {data, statusCode} = yield call(() => request.post('/my/fleet/add', {
        carNumber: payload?.carNumber,
        vin: payload?.vin,
        carPhoneNumber: payload?.carPhoneNumber,
        fleetId: fleetAccountId
      }));
      console.log('addCar', data);
      if (statusCode && statusCode === 200 && data.code === 200){
        Taro.showToast({
          title: '新增成功',
          icon: 'success',
          duration: 1500
        })
        yield call(() => delay(1000));
        Taro.navigateBack({
          delta: 1
        })
        yield put({
          type: 'init',
          payload: {
            fleetAccountId
          }
        })
      } else {
        Taro.showToast({
          title: data?.msg,
          icon: 'none',
          duration: 1500
        });
      }
    },
    *editCar({payload}, {call, put, select}){
      console.log('payload', payload);
      const {currentCar = {}, fleetAccountId} = yield select(({fleet}) => fleet)
      const {data, statusCode} = yield call(() => request.post('/my/fleet/update', {
        carNumber: payload?.carNumber,
        vin: payload?.vin,
        carPhoneNumber: payload?.carPhoneNumber,
        id: currentCar.id || ''
      }));
      console.log('editCar', data);
      if (statusCode && statusCode === 200 && data.code === 200){
        Taro.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1500
        });
        yield call(() => delay(1000))
        Taro.navigateBack({
          delta: 1
        })
        yield put({
          type: 'init',
          payload: {
            fleetAccountId
          }
        })
      } else {
        Taro.showToast({
          title: data?.msg,
          icon: 'none',
          duration: 1500
        });
      }
    },
    *deleteCar({payload}, {call, put, select}){
      const {carId} = payload;
      const {fleetAccountId} = yield select(({fleet}) => fleet);
      console.log('carId', carId);
      console.log('fleetAccountId', fleetAccountId);
      const {data, statusCode} = yield call(() => request.post('/my/fleet/delete', {id: carId}));
      console.log('删除回调', data);
      if (statusCode && statusCode === 200 && data.code === 200){
        Taro.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1500
        });
        yield call(() => delay(1000))
        yield put({
          type: 'init',
          payload: {
            fleetAccountId
          }
        })
      } else {
        Taro.showToast({
          title: data?.msg,
          icon: 'none',
          duration: 1500
        });
      }
    }
  },
  reducers: {
    setState(state, {payload}){
        return {
          ...state,
          ...payload
        }
    }
  }
}
